package com.app.fenak;

import com.app.fenak.validation.ExpressionValidation;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class SignUpActivity extends Activity {

	private EditText firstname;
	private EditText lastname;
	private EditText username;
	private EditText password;
	private EditText confirmPassword;
	private EditText email;
	private Button signUpButton;
	private TextView firstnameError;
	private TextView lastnameError;
	private TextView passwordError;
	private TextView confirmPasswordError;
	private TextView emailError;
	private TextView usernameError;
	private ImageView usernameState;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(R.layout.sign_up);

		firstname = (EditText) findViewById(R.id.first_name_field);
		lastname = (EditText) findViewById(R.id.last_name_field);
		username = (EditText) findViewById(R.id.user_name_field);
		password = (EditText) findViewById(R.id.password_field);
		confirmPassword = (EditText) findViewById(R.id.confirm_password_field);
		email = (EditText) findViewById(R.id.email_field);
		signUpButton = (Button) findViewById(R.id.sign_up_button);

		firstnameError = (TextView) findViewById(R.id.firstname_error);
		lastnameError = (TextView) findViewById(R.id.lastname_error);
		passwordError = (TextView) findViewById(R.id.password_error);
		confirmPasswordError = (TextView) findViewById(R.id.confirm_password_error);
		emailError = (TextView) findViewById(R.id.email_error);
		usernameError = (TextView) findViewById(R.id.username_error);
		usernameState = (ImageView) findViewById(R.id.username_state);
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();

		email.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if( !(confirmPassword.getText().toString().equalsIgnoreCase(password.getText().toString().toLowerCase()))){
					confirmPasswordError.setVisibility(View.VISIBLE);
					confirmPasswordError.setText("The passwords don't match");
				}else{
					confirmPasswordError.setVisibility(View.GONE);
				}
				
			}
		});
		signUpButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				validate();
			}
		});

	}

	private boolean validate() {

		String firstnameString = firstname.getText().toString();
		String lastnameString = lastname.getText().toString();
		String usernameString = username.getText().toString();
		String passwordString = password.getText().toString();
		String confirmPasswordString = confirmPassword.getText().toString();
		String emailString = email.getText().toString();

		boolean returnCheck = false;

		if (firstnameString.equalsIgnoreCase("")) {

			firstnameError.setVisibility(View.VISIBLE);
			firstnameError.setText("Please enter your first name");

		} else {
			firstnameError.setVisibility(View.GONE);
		}
		if (lastnameString.equalsIgnoreCase("")) {
			lastnameError.setVisibility(View.VISIBLE);
			lastnameError.setText("Please enter your last name");

		} else {
			lastnameError.setVisibility(View.GONE);
		}
		
		
		if (usernameString.equalsIgnoreCase("")) {
			usernameError.setVisibility(View.VISIBLE);
			usernameError.setText("Please enter your username");
		} else if (usernameString.contains(" ")) {
			usernameError.setVisibility(View.VISIBLE);
			usernameError.setText("Please don't put spaces in the username");
		} 
//		else if (false) { // here i will check whether this username is available or not
//			
//			usernameState.setVisibility(View.VISIBLE);
//			usernameState.setImageDrawable(getResources().getDrawable(R.drawable.error));  // if not available
//			usernameState.setImageDrawable(getResources().getDrawable(R.drawable.valid));  // if  available
//		}
		else {
			usernameError.setVisibility(View.GONE);
		}
		if (emailString.equalsIgnoreCase("")) {
			emailError.setVisibility(View.VISIBLE);
			emailError.setText("Please enter your email");
		} else if (!ExpressionValidation.IsMatch(emailString,
				ExpressionValidation.EMAIL_PATTERN)) {

			emailError.setVisibility(View.VISIBLE);
			emailError.setText("Please enter email in correct format");
		} else {
			emailError.setVisibility(View.GONE);
		}

		return returnCheck;
	}
}
